//
//  HttpRequest.m
//  museum
//
//  Created by thomasho on 15/11/26.
//  Copyright © 2015年 thomasho. All rights reserved.
//

#import "HttpRequest.h"
#import <AFNetworking.h>
#import "NSString+expanded.h"

@implementation HttpRequest

+ (NSMutableDictionary *) parse:(NSString *)url data:(NSDictionary *)dict {
    NSString *token = SF2(@"%@%@11oewkdj8bunl4n3ystcxes3xjgiuac2",url,[[Utility timeInBeijing] substringToIndex:10]);
    NSMutableDictionary *mdict = [NSMutableDictionary dictionaryWithDictionary:dict];
    [mdict setValue:@"2878804224" forKey:@"app_key"];
    [mdict setValue:token.md5 forKey:@"api_token"];
    if ([dict valueForKey:@"uid"]) {
        [mdict setValue:[[Utility defaultsForKey:@"userInfo"] valueForJSONStrKey:@"user_token"]forKey:@"user_token"];
        [mdict setValue:[[Utility defaultsForKey:@"userInfo"] valueForJSONStrKey:@"uid"] forKey:@"uid"];
    }
    return mdict;
}

//参数会默认追加uid和user_token
+ (void) request:(NSString *)url data:(NSDictionary *)dict block:(void(^)(id,id))block{
    [[AFHTTPRequestOperationManager manager] POST:SF2(@"%@%@", baseDomain, url) parameters:[self parse:url data:dict] success:^(AFHTTPRequestOperation * operation, id  responseObject) {
        NSInteger code = [[responseObject valueForKey:@"code"] integerValue];

        if (code==10000) {
            if(block)block([responseObject valueForKey:@"result"],nil);
        }else if(code == 20001){
            [[Utility Share] alert:NSLocalizedString(@"您的账号已经在另外的设备上登录，为了您的账号安全，请重新登录！", nil) delegate:[UIApplication sharedApplication].delegate];
            if(block)block(nil,[responseObject valueForKey:@"msg"]);
        }else if(code == 10011||code == 10012){
            [[Utility Share] alert:NSLocalizedString(@"您的软件已经无法继续使用，请更新最新版本", nil) delegate:[UIApplication sharedApplication].delegate buttons:@[NSLocalizedString(@"关闭",nil)] tag:100];
            if(block)block(nil,[responseObject valueForKey:@"msg"]);
        }else{
            if(block)block(nil,[responseObject valueForKey:@"msg"]);
        }
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        if(block)block(nil,error.localizedDescription);
    }];
}

//上传头像
+ (void)request:(NSString *)url data:(NSDictionary *)dict file:(UIImage *)image block:(void (^)(id, id))block {
    [[AFHTTPRequestOperationManager manager] POST:url parameters:[self parse:url data:dict] constructingBodyWithBlock:^(id<AFMultipartFormData>  formData) {
        [formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.9) name:@"avator" fileName:@"avator" mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([[responseObject valueForKey:@"code"] integerValue]==10000) {
            if(block)block([responseObject valueForKey:@"result"],nil);
        }else{
            if(block)block(nil,[responseObject valueForKey:@"msg"]);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if(block)block(nil,error.localizedDescription);
    }];
}

@end
